package com.ruoyi.student.service.impl;

import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.student.domain.StudentFormDto;
import com.ruoyi.student.domain.SubjectFormDto;
import com.ruoyi.student.domain.SysClass;
import com.ruoyi.student.mapper.SysClassMapper;
import com.ruoyi.teacher.api.domain.Campus;
import com.ruoyi.teacher.api.domain.Classes;
import com.ruoyi.teacher.api.domain.Subject;
import com.ruoyi.teaching.api.RemoteTeachingService;
import com.ruoyi.teaching.api.domain.FocusStudentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.student.mapper.SysStudentMapper;
import com.ruoyi.student.domain.SysStudent;
import com.ruoyi.student.service.ISysStudentService;

/**
 * 学生管理Service业务层处理
 *
 * @author Xsw
 * @date 2024-03-23
 */
@Service
public class SysStudentServiceImpl implements ISysStudentService {
    @Autowired
    private SysStudentMapper sysStudentMapper;
    @Autowired
    private SysClassMapper classMapper;
    @Autowired
    private RemoteTeachingService remoteTeachingService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询学生管理
     *
     * @param id 学生管理主键
     * @return 学生管理
     */
    @Override
    public SysStudent selectSysStudentById(Long id) {
        return sysStudentMapper.selectSysStudentById(id);
    }

    /**
     * 查询学生管理列表
     *
     * @param sysStudent 学生管理
     * @return 学生管理
     */
    @Override
    public List<SysStudent> selectSysStudentList(SysStudent sysStudent) {
        redisTemplate.delete("studentList");
        List<SysStudent> sysStudentList = sysStudentMapper.selectSysStudentList(sysStudent);
        // 将学生列表保存到redis中
        redisService.deleteObject("studentList");
        redisService.setCacheList("studentList", sysStudentList);
        return sysStudentMapper.selectSysStudentList(sysStudent);
    }

    /**
     * 新增学生管理
     *
     * @param sysStudent 学生管理
     * @return 结果
     */
    @Override
    public int insertSysStudent(SysStudent sysStudent) {
        return sysStudentMapper.insertSysStudent(sysStudent);
    }

    /**
     * 修改学生管理
     *
     * @param sysStudent 学生管理
     * @return 结果
     */
    @Override
    public int updateSysStudent(SysStudent sysStudent) {
        return sysStudentMapper.updateSysStudent(sysStudent);
    }

    /**
     * 批量删除学生管理
     *
     * @param ids 需要删除的学生管理主键
     * @return 结果
     */
    @Override
    public int deleteSysStudentByIds(Long[] ids) {
        return sysStudentMapper.deleteSysStudentByIds(ids);
    }

    /**
     * 删除学生管理信息
     *
     * @param id 学生管理主键
     * @return 结果
     */
    @Override
    public int deleteSysStudentById(Long id) {
        return sysStudentMapper.deleteSysStudentById(id);
    }

    @Override
    public void updateTypes(List<SysStudent> list) {
        list.forEach(sysStudent -> sysStudentMapper.updateTypes(sysStudent));
    }

    @Override
    public void importStudent(List<SysStudent> students) {
        sysStudentMapper.batchInsert(students);
    }

    @Override
    public List<SysStudent> getStudentByClassId(Long classId) {
        return sysStudentMapper.getStudentByClassId(classId);
    }

    @Override
    public void addStudentFocus(FocusStudentDto focusStudentDto) {
        remoteTeachingService.addStudentFocus(focusStudentDto);
    }

    @Override
    public List<SysStudent> unemployedStudentList() {
        return sysStudentMapper.unemployedStudentList();
    }

    @Override
    public List<Campus> getCampusList() {
        List<SysClass> sysClassList = classMapper.selectSysClassList(new SysClass());
        List<Campus> campusList = redisService.getCacheList("campus");
        HashMap<String, List<Classes>> map = new HashMap<>();
        campusList.forEach(m -> {
            Long campusId = m.getId();
            for (Subject subject : m.getSubjects()) {
                map.put(campusId + "-" + subject.getId(), subject.getClasses());
            }
        });
        sysClassList.forEach(sl -> {
            Optional.ofNullable(map.get(sl.getCampusId() + "-" + sl.getSubjectId())).ifPresent(list -> {
                list.add(BeanUtil.copyProperties(sl, Classes.class));
            });
        });
        campusList.forEach(campus -> {
            List<Subject> subjects = campus.getSubjects();
            subjects.forEach(subject -> {
                subject.getId();
            });
        });
        return campusList;
    }

    @Override
    public int changeStatusById(Long studentId) {
        return sysStudentMapper.changeStatusById(studentId);
    }

    @Override
    public int repeatedChange(SysStudent sysStudent) {
        return sysStudentMapper.repeatedChange(sysStudent);
    }

    @Override
    public ArrayList<StudentFormDto> getStudentsByFormData(StudentFormDto studentFormDto) {
        List<SysClass> classList = classMapper.getClassByStudentFormData(studentFormDto);
        HashMap<LocalDate, Long> map = new HashMap<>();
        classList.forEach(classes -> {
            Long classId = classes.getId();
            studentFormDto.setClassId(classId);
            List<SysStudent> students = sysStudentMapper.getStudentsByFormData(studentFormDto);
            for (SysStudent s : students) {
                Date createTime = s.getCreateTime();
                LocalDate date = createTime.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate();
                if (map.containsKey(date)) {
                    Long value = map.get(date);
                    map.put(date, value + 1l);
                } else {
                    map.put(date, 1l);
                }
            }
        });
        ArrayList<StudentFormDto> studentFormDtos = new ArrayList<>();
        for (Map.Entry<LocalDate, Long> entry : map.entrySet()) {
            StudentFormDto newStudentFormDto = new StudentFormDto();
            LocalDate key = entry.getKey();
            Long value = entry.getValue();
            newStudentFormDto.setTableTime(key);
            newStudentFormDto.setNumberOfStudent(value);
            studentFormDtos.add(newStudentFormDto);
        }
        return studentFormDtos;
    }

    @Override
    public ArrayList<SubjectFormDto> getStudentsByMouth(SubjectFormDto subjectFormDto) {
        ArrayList<SubjectFormDto> al = new ArrayList<>();
        List<SysClass> sysClassList = classMapper.selectSysClassList(new SysClass());
        // 给班级按照学科id分类
        Map<Long, List<SysClass>> classBySubjectId = sysClassList.stream()
                .collect(Collectors.groupingBy(SysClass::getSubjectId));
        // 这是java学科的班级
        List<SysClass> class1 = classBySubjectId.get(1l);
        SubjectFormDto subjectFormDto1 = new SubjectFormDto();
        subjectFormDto1.setSubject("Java学科");
        class1.forEach(classes -> {
            Long id = classes.getId();
            List<SysStudent> studentByClassId = sysStudentMapper.getStudentByClassId(id);
            studentByClassId.forEach(sysStudent -> {
                Date createTime = sysStudent.getCreateTime();
                if (subjectFormDto.getMonth().getMonth() == createTime.getMonth()) {
                    subjectFormDto1.setNumberOfStudent(subjectFormDto1.getNumberOfStudent() + 1l);
                }
            });
        });
        al.add(subjectFormDto1);
        // 这是前端学科的班级
        List<SysClass> class2 = classBySubjectId.get(2l);
        SubjectFormDto subjectFormDto2 = new SubjectFormDto();
        subjectFormDto2.setSubject("H5前端学科");
        class2.forEach(classes -> {
            Long id = classes.getId();
            List<SysStudent> studentByClassId = sysStudentMapper.getStudentByClassId(id);
            studentByClassId.forEach(sysStudent -> {
                Date createTime = sysStudent.getCreateTime();
                if (subjectFormDto.getMonth().getMonth() == createTime.getMonth()) {
                    subjectFormDto2.setNumberOfStudent(subjectFormDto2.getNumberOfStudent() + 1l);
                }
            });
        });
        al.add(subjectFormDto2);
        // 这是测试学科的班级
        List<SysClass> class3 = classBySubjectId.get(3l);
        SubjectFormDto subjectFormDto3 = new SubjectFormDto();
        subjectFormDto3.setSubject("测试学科");
        class3.forEach(classes -> {
            Long id = classes.getId();
            List<SysStudent> studentByClassId = sysStudentMapper.getStudentByClassId(id);
            studentByClassId.forEach(sysStudent -> {
                Date createTime = sysStudent.getCreateTime();
                if (subjectFormDto.getMonth().getMonth() == createTime.getMonth()) {
                    subjectFormDto3.setNumberOfStudent(subjectFormDto3.getNumberOfStudent() + 1l);
                }
            });
        });
        al.add(subjectFormDto3);
        return al;
    }

    @Override
    public List<SysStudent> getStudentList() {
        return sysStudentMapper.getStudentList();
    }
}
